﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;

namespace NJL.WebUsers
{
    public partial class SecondLogin : BasePageVisitor
    {
        public string AUTH_GUID = "";

        protected string FROM_WEBSITE_CODE = "";
        protected string FROM_DOMAIN = "";
        protected string FROM_URL = "";

        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();

        protected void Page_Load(object sender, EventArgs e)
        { 
   
            if (!IsPostBack)
            {
                hidFromWebSiteCode.Value = Request.QueryString["websitecode"];
                hidFromDomain.Value = Request.QueryString["domain"];
                hidFromURL.Value = Request.QueryString["url"];

                FROM_WEBSITE_CODE = hidFromWebSiteCode.Value;
                FROM_DOMAIN = hidFromDomain.Value;
                FROM_URL = hidFromURL.Value;
                //LoadPageSEO();
            }
        }
        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            //Page.Title = "用户登录-" + WEBSITE_SYS_BASE.WEBSITE_NAME;

            //HtmlMeta myMeta = new HtmlMeta();
            //myMeta.Name = "Keywords";
            //myMeta.Content = WEBSITE_SYS_BASE.WEBSITE_KEYWORDS;
            //this.Header.Controls.Add(myMeta);

            //HtmlMeta myMeta1 = new HtmlMeta();
            //myMeta1.Name = "Description";
            //myMeta1.Content = WEBSITE_SYS_BASE.WEBSITE_DESCRIPTION;
            //this.Header.Controls.Add(myMeta1);
        }

        protected void btnEmail_Click(object sender, EventArgs e)
        {
            string userName = txtEmail.Text;
            string pwd = hidEmail.Value;
            DataSet ds = bllUser.GetUserLoginByEmail(userName, pwd);
            if (ds.Tables[0].Rows.Count > 0)
            {

                Session.Clear();
                Session.Abandon();
                Session.Add("userid", ds.Tables[0].Rows[0]["ID"]);
                Session.Add("username", ds.Tables[0].Rows[0]["USER_NAME"]);
                Session.Add("usertype", ds.Tables[0].Rows[0]["TYPE_CODE"]);
                string userType = ds.Tables[0].Rows[0]["TYPE_CODE"].ToString();
                string strToURL = "";
                //如果未激活先到激活页面
                if (userType == "-1")
                {
                    strToURL = "/RegisterActive.aspx";
                }
                else if (Request.QueryString["url"] == null)
                {
                    strToURL = "/";
                }
                else if (Request.QueryString["url"] != null)
                {
                    strToURL = hidFromURL.Value;
                }


                ShareSession.ShareSessionFormatter.Serialize(ShareSession.ShareSessionFormatter.FormatterType.Soap);
                AUTH_GUID = Session["AUTH_GUID"].ToString();

                string strURL = "http://" + hidFromDomain.Value + "/GetUserSessions.aspx?AUTH_GUID=" + AUTH_GUID + "&logintype=opendiv&tourl=" + strToURL;
                Response.Redirect(strURL, true);

                //Session.Add("userid", ds.Tables[0].Rows[0]["ID"]);
                //Session.Add("username", ds.Tables[0].Rows[0]["USER_NAME"]);
                //Session.Add("usertype", ds.Tables[0].Rows[0]["TYPE_CODE"]);
                //string userType = ds.Tables[0].Rows[0]["TYPE_CODE"].ToString();
                ////如果未激活先到激活页面
                //if (userType == "-1")
                //{
                //    Response.Redirect("~/RegisterActive.aspx");
                //}
                //else if (Request.QueryString["url"] == null)
                //{
                //    Response.Redirect("~/");
                //}
                //else if (Request.QueryString["url"] != null)
                //{
                //    Page.ClientScript.RegisterStartupScript(GetType(), "mes", "<script>top.window.location='"+Request.QueryString["url"]+"'</script>");
                //}
            }
            else
            {
                Royalsoft.Common.MessageBox.Show(this, "请输入正确的账号或密码");
            }
        }
    }
}